home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 401-425 / disk_415 / cbbs / cbbs.lzh / mem / saveram.c < prev   
C/C++ Source or Header  |  1990-03-23  |  6KB  |  253 lines

  1. #include <ctype.h>
  2. #include <libraries/dos.h>
  3. #include <exec/memory.h>
  4. #include <time.h>
  5. #include <stdio.h>
  6.  
  7. /* SAVERAM  [-q] [ramdir] [diskdir]
  8. */
  9. /* There's a problem with the Rename function. It does not change the
  10.    file date if you rename a file. So if a file in the ram: directory
  11.    is renamed and then you do a saveram, the saveram program won't
  12.    detect that a change has been made to the file.
  13.    RATS
  14.  
  15.    28-jul-89 Modified so that it only gets a lock on the output directory
  16.              if it actually needs to save a file (see the 'nothing' variable)
  17. */
  18.  
  19. /*
  20.  *  A directory item looks like this
  21. struct FileInfoBlock {
  22.    LONG fib_DiskKey;
  23.    LONG fib_DirEntryType;
  24.    char fib_FileName[108];
  25.    LONG fib_Protection;
  26.    LONG fib_EntryType;
  27.    LONG fib_Size;
  28.    LONG fib_NumBlocks;
  29.    struct DateStamp fib_Date;
  30.    char fib_Comment[80];
  31.    char padding[36];
  32. };
  33. */
  34.  
  35. typedef struct FileInfoBlock FCB;
  36. FCB *ifcb = 0,*ofcb = 0;
  37. static int olddta;
  38. long ilock = 0L,olock = 0L;
  39. long date[3];
  40. short qflag = 0;
  41. char dirname[100];
  42. char ramname[100];
  43. char loadname[30];
  44. short nothing = 1;
  45. main(argc,argv)
  46. int argc;
  47. char *argv[];
  48. {
  49.    FILE *fd;
  50.    char *fp,*cp,*rp;
  51.    extern char *filename();
  52.    extern char *AllocMem();
  53.    extern long diropen();
  54.    /* This allocates the memory on a longword boundary */
  55.    ifcb = (FCB *)AllocMem((long)sizeof(*ifcb),
  56.                                    (long)MEMF_PUBLIC|MEMF_CLEAR);
  57.    ofcb = (FCB *)AllocMem((long)sizeof(*ofcb),
  58.                                    (long)MEMF_PUBLIC|MEMF_CLEAR);
  59.    if(argc > 1) {
  60.       if(argv[1][0] == '-') {
  61.          if(argv[1][1] == 'q') {
  62.             qflag++;
  63.          }
  64.          argc--;
  65.          argv++;
  66.       }
  67.    }
  68.    if(argc > 3)usage();
  69.    if(argc < 2)
  70.       rp = ".loadram";
  71.    else
  72.       rp = filename(argv[1],".loadram");
  73.    for(cp=&loadname[0];*rp;)*cp++ = *rp++;
  74.    *cp++ = 0;
  75.    if((fd = fopen(&loadname[0],"r")) == 0) {
  76.       printf("Can't find %s\n",&loadname[0]);
  77.       done();
  78.    }
  79.    /* read the date from the file  */
  80.    fread(&date[0],12,1,fd);
  81.    /* and the target directory */
  82.    fread(&dirname[0],100,1,fd);
  83.    /* and the ram directory */
  84.    fread(&ramname[0],100,1,fd);
  85.    fclose(fd);
  86.    /* now override the disk directory if it's been specified as an arg */
  87.    if(argc > 2) {
  88.       for(fp=dirname,cp=argv[2];*cp;)*fp++ = *cp++;
  89.       *fp++ = 0;
  90.    }
  91.    if((ilock = diropen(&ramname[0],ifcb)) == 0L) {
  92.       printf("Can't open %s\n",&ramname[0]);
  93.       done();
  94.    }
  95.    UnLock(olock);
  96.    olock = 0L;
  97.    /* Look through the directory and copy anything that's changed */
  98.    while(dirnext(ifcb,ilock)) {
  99.       if(later(&ifcb->fib_Date,&date[0])) {
  100.          if(nothing && ((olock = diropen(&dirname[0],ofcb)) == 0L)) {
  101.             printf("Can't open %s\n",&dirname[0]);
  102.             done();
  103.          }
  104.          nothing = 0;
  105.          copy(&ramname[0],&dirname[0]);
  106.          if(!qflag)printf("%s\n",ifcb->fib_FileName);
  107.       }
  108.    }
  109.    ilock = 0L;
  110.    if((fd = fopen(&loadname[0],"r+")) == 0) {
  111.       printf("Can't find %s\n",loadname);
  112.       done();
  113.    }
  114.    /* write the current date in the file and we're done */
  115.    DateStamp(&date[0]);
  116.    fwrite(&date[0],12,1,fd);
  117.    fclose(fd);
  118.    done();
  119. }
  120. done()
  121. {
  122.    freefcb();
  123.    if(ilock)UnLock(ilock);
  124.    if(olock)UnLock(olock);
  125.    exit(0);
  126. }
  127. static char filestr[200];
  128. /* append string b to a to create a filename */
  129. char *filename(a,b)
  130. char *a,*b;
  131. {
  132.    register char *p,*q;
  133.    p = filestr;
  134.    q = a;
  135.    while(*q)*p++ = *q++;
  136.    q--;
  137.    if(*q != ':')*p++ = '/';
  138.    q = b;
  139.    while(*q)*p++ = *q++;
  140.    *p = 0;
  141.    return(&filestr[0]);
  142. }
  143. usage()
  144. {
  145.    printf("usage: saveram [[[-q] ramdir] diskdir]\n");
  146.    printf("-q   don't print filenames\n");
  147.    freefcb();
  148. }
  149.  
  150. /*
  151.  *  Open directory and return a Lock or zero, if it fails the lock is
  152.  *  unlocked
  153.  */
  154. extern long Lock(),Examine(),ExNext();
  155. long diropen(cp,fcb)
  156. char   *cp;
  157. FCB *fcb;
  158. {
  159.    long  result,lock;
  160.    /* Get the lock on the directory */
  161.    lock = Lock(cp,ACCESS_READ);
  162.    if(lock == 0L) {
  163.       return (0L);
  164.    }
  165.    /* Examine the lock and if that fails or if this is not a directory
  166.       then give up
  167.    */
  168.    result = Examine(lock,fcb);
  169.    if((result == 0L) || (fcb->fib_DirEntryType < 0)) {
  170.       UnLock(lock);
  171.       return(0L);
  172.    }
  173.    /* Looks good */
  174.    return(lock);
  175. }
  176. freefcb()
  177. {
  178.    if(ifcb)FreeMem(ifcb,(long)sizeof(*ifcb));
  179.    if(ofcb)FreeMem(ofcb,(long)sizeof(*ofcb));
  180. }
  181. /*
  182.  *  Return next file entry, ignore directories and when the lock
  183.  *  is exhausted Unlock it.
  184.  */
  185.  
  186. dirnext(fcb,lock)
  187. FCB *fcb;
  188. long lock;
  189. {
  190.    /* search for next file */
  191.    while(1) {
  192.       if(ExNext(lock,fcb) == 0L) {
  193.          UnLock(lock);
  194.          return(0);
  195.       }
  196.       /* If it is a directory ignore it */
  197.       if(fcb->fib_DirEntryType >= 0) continue;
  198.       /* If it is the .loadram file bypass it */
  199.       if(match(".loadram",&fcb->fib_FileName[0]))continue;
  200.       return(1);
  201.    }
  202. }
  203. /* copy fib_FileName from inf to outf */
  204. char iobuf[512];
  205. copy(inf,outf)
  206. char *inf,*outf;
  207. {
  208.    FILE *fdi,*fdo;
  209.    int n;
  210.    if((fdi = fopen(filename(inf,&ifcb->fib_FileName),"r")) == NULL) {
  211.       printf("Can't open %s\n",filestr);
  212.       return;
  213.    }
  214.    if((fdo = fopen(filename(outf,&ifcb->fib_FileName),"w")) == NULL) {
  215.       printf("Can't open %s\n",filestr);
  216.       return;
  217.    }
  218.    while(n = fread(iobuf,1,512,fdi)) {
  219.       fwrite(iobuf,n,1,fdo);
  220.    }
  221.    fclose(fdi);
  222.    fclose(fdo);
  223. }
  224. later(d1,d2)
  225. long d1[],d2[];
  226. {
  227.    if(d1[0] > d2[0])return(1);
  228.    if(d1[0] < d2[0])return(0);
  229.    if(d1[1] > d2[1])return(1);
  230.    if(d1[1] < d2[1])return(0);
  231.    if(d1[2] > d2[2])return(1);
  232.    return(0);
  233. }
  234. match(a,b)
  235. char *a,*b;
  236. {
  237.    register char *p,*q;
  238.  
  239.    p = a;
  240.    q = b;
  241.    while(*p && *q && (*q != '*')) {
  242.       if(*p == *q) {
  243.          p++;
  244.          q++;
  245.          continue;
  246.       }
  247.       break;
  248.    }
  249.    if((*p == 0) && (*q == 0))return(1);
  250.    if(*q == '*')return(1);
  251.    return(0);
  252. }
  253.